package com.leet.code.lcof;

public class Code16 {
    public static void main(String[] args) {
        Code16 code16 = new Code16();
        System.out.println(code16.pow(8.84372, -5));
        System.out.println(code16.pow(2, 10));
    }

    public double myPow(double x, int n) {
        if (x == 0) {
            return 0;
        }

        if (n == 0 || x == 1) {
            return 1;
        }
        if (n == 1) {
            return x;
        }
        boolean f = false;
        if (n < 0) {
            n = -n;
            f = true;
        }

        double pow = x;
        int i = 1;
        int z = n >> 1;
        while (i <= z) {
            pow = pow * pow;
            i = i << 1;
        }
        pow = pow * myPow(x, n - i);
        if (f) {
            pow = 1 / pow;
        }
        return pow;
    }

    public double pow(double x, int n) {
        if (x == 0) {
            return 0;
        }
        if (n == 0 || x == 1) {
            return 1;
        }
        if (n == 1) {
            return x;
        }
        long b = n;
        if (b < 0) {
            x = 1 / x;
            b = -b;
        }

        double pow = 1.0;
        while (b > 0) {
            if ((b & 1) == 1) {
                pow = pow * x;
            }
            x = x * x;

//            pow = x;
            b = b >> 1;

        }
        return pow;

    }

}
